package com.example.demo.controller;

import com.example.demo.annotation.UserLoginToken;
import com.example.demo.entity.request.IdList;
import com.example.demo.entity.request.PushBlogEntity;
import com.example.demo.entity.request.UpdateBlog;
import com.example.demo.entity.response.BlogEntity;
import com.example.demo.service.TbBlogService;
import com.example.demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("博客后台API")
@RestController
@RequestMapping("/admin/blog")
public class AdminBlogController {
    TbBlogService blogService;

    @Autowired
    public void setBlogService(TbBlogService blogService) {
        this.blogService = blogService;
    }

    @UserLoginToken
    @ApiOperation("发布博客")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "content", value = "内容"),
            @ApiImplicitParam(name = "tagNames", value = "标签id列表")
    })
    @PostMapping("pushBlog")
    public Result pushBlog(@RequestBody @Validated PushBlogEntity pushBlogEntity) {
        String title = pushBlogEntity.getTitle();
        String content = pushBlogEntity.getContent();
        String[] tagNames = pushBlogEntity.getTagNames();
        blogService.pushBlog(title, content, tagNames);
        return new Result(0, "success");
    }

    @UserLoginToken
    @ApiOperation("通过id列表删除多篇文章")
    @ApiImplicitParam(name = "ids", value = "文章id列表")
    @PostMapping("deleteBlogById")
    public Result deleteBlogById(@RequestBody @Validated IdList idList) {
        blogService.deleteBlogById(idList.getIds());
        return new Result(0, "success");
    }

    @UserLoginToken
    @ApiOperation("通过id更新文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id"),
            @ApiImplicitParam(name = "changeContent", value = "true：修改的是内容，false：修改标题"),
            @ApiImplicitParam(name = "text")
    })
    @PostMapping("updateBlogById")
    public Result updateBlogById(@RequestBody @Validated UpdateBlog updateBlog) {
        int id = updateBlog.getId();
        boolean changeContent = updateBlog.isChangeContent();
        String text = updateBlog.getText();
        blogService.updateBlogById(id, changeContent, text);
        return new Result(0, "success");
    }

    @ApiOperation("分页获取博客文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "index", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "页大小")
    })
    @GetMapping("getBlogsByPage")
    public Result getBlogsByPage(Integer index, Integer size) {
        Map result = blogService.getBlogsByPage(index, size);
        return new Result(0, "success", result);
    }

    @ApiOperation("根据标签查找文章")
    @ApiImplicitParam(name = "tagName", value = "标签名")
    @GetMapping("getBlogByTagName")
    public Result getBlogByTagName(String tagName, Integer index, Integer size) {
        Map<String, Object> blogEntityList = blogService.getBlogsByTagName("%" + tagName + "%", index, size);
        return new Result(0, "success", blogEntityList);
    }

    @ApiOperation("获取单篇文章评论列表")
    @ApiImplicitParam(name = "id", value = "文章ID")
    @GetMapping("getBlogMessageById")
    public Result getBlogMessageById(Integer id, Integer index, Integer size) {
        if(id == null) return new Result(-1, "缺少参数");
        Map<String, Object> message = blogService.getBlogMessageById(id, index, size);
        return new Result(0, "success", message);
    }

    @UserLoginToken
    @ApiOperation("通过id删除文章单条评论")
    @ApiImplicitParam(value = "id", name = "评论id")
    @PostMapping("deleteMessageForBlogById")
    public Result deleteMessageForBlogById(@RequestParam("id") Integer id) {
        blogService.deleteMessageForBlogById(id);
        return new Result(0, "success");
    }

}
